In [1]:
%pylab inline
from classy import *


Populating the interactive namespace from numpy and matplotlib
Version:  0.0.15

In [2]:
images=image.load_images('data/digits')


[0]: 178 files found
[1]: 182 files found
[2]: 177 files found
[3]: 183 files found
[4]: 181 files found
[5]: 182 files found
[6]: 181 files found
[7]: 179 files found
[8]: 174 files found
[9]: 180 files found

In [3]:
data=image.images_to_vectors(images)
data.vectors-=data.vectors.mean()
data.vectors/=data.vectors.std()


1797 vectors of length 64
Feature names: 'p0', 'p1', 'p2', 'p3', 'p4'  , ... ,  'p59', 'p60', 'p61', 'p62', 'p63'  (64 features)
Target values given.
Target names: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
Mean:  [  0.00000000e+00   4.71341124e+00   8.25158598e+01   1.88401224e+02
   1.88589872e+02   9.17840846e+01   2.15581525e+01   2.04785754e+00
   8.51419032e-02   3.14913745e+01   1.65203116e+02   1.90677240e+02
   1.63504174e+02   1.30023372e+02   2.92036728e+01   1.70005565e+00
   4.22927101e-02   4.11185309e+01   1.57575960e+02   1.11080690e+02
   1.12814691e+02   1.24173066e+02   2.82609905e+01   7.82971619e-01
   1.66944908e-02   3.90011130e+01   1.44642181e+02   1.40286589e+02
   1.58007791e+02   1.20022816e+02   3.66889260e+01   3.33889816e-02
   0.00000000e+00   3.70083472e+01   1.21928770e+02   1.44347245e+02
   1.63978854e+02   1.39081803e+02   4.60222593e+01   0.00000000e+00
   1.37451308e-01   2.49860879e+01   1.09460768e+02   1.14961046e+02
   1.21993879e+02   1.30974958e+02   5.47762938e+01   4.24040067e-01
   1.13522538e-01   1.10378408e+01   1.19312187e+02   1.51698943e+02
   1.49728993e+02   1.39340011e+02   5.90962716e+01   3.23873122e+00
   8.34724541e-03   4.34780189e+00   8.81591541e+01   1.92462994e+02
   1.88009460e+02   1.07495826e+02   3.27490262e+01   5.77072899e+00]
Median:  [   0.    0.   63.  207.  207.   63.    0.    0.    0.    0.  191.  207.
  175.  143.    0.    0.    0.   15.  191.   95.   95.  127.    0.    0.
    0.   15.  175.  159.  191.  111.    0.    0.    0.    0.  127.  159.
  207.  159.   15.    0.    0.    0.   95.  111.  127.  143.   15.    0.
    0.    0.  127.  159.  159.  159.   15.    0.    0.    0.   63.  207.
  223.   95.    0.    0.]
Stddev:  [   0.           14.22717752   75.79479369   67.88517638   68.52232671
   90.3581085    52.88625131   16.47216306    1.44842195   50.7576493
   86.55422116   63.60290344   76.42760101   96.53740043   57.02565325
   13.11647513    0.95321433   56.84679424   90.80932368   92.58050776
   98.50000012   98.78779801   51.79075122    6.90387772    0.50013864
   49.94970201   98.7781811    93.88418435   98.12528728   93.67899482
   58.58726878    0.70690869    0.           55.28329868  100.86027538
   99.97913895   94.64396922   93.64015167   56.19204049    0.
    2.26156209   47.3537032   104.20244968  102.68526906   99.83278433
   90.82739038   68.89733343    4.82912707    3.23145722   27.62316404
   90.02004928   83.47815394   84.68687626   96.17533855   78.31457884
   15.55049191    0.35374992   14.68333191   81.36743295   69.86967274
   78.76953994   94.07347801   65.09626368   29.57005528]

In [4]:
data_train,data_test=split(data)
image.vector_to_image(data_train.vectors[800,:],(8,8))


Original vector shape:  (1797, 64)
Train vector shape:  (1347, 64)
Test vector shape:  (450, 64)

Do Perceptron First


In [5]:
C=Perceptron()

In [6]:
timeit(reset=True)
C.fit(data_train.vectors,data_train.targets)
print(("Training time: ",timeit()))


Time Reset
('Training time: ', '0.07055115699768066 seconds ')

In [7]:
print(("On Training Set:",C.percent_correct(data_train.vectors,data_train.targets)))
print(("On Test Set:",C.percent_correct(data_test.vectors,data_test.targets)))


('On Training Set:', 98.886414253897541)
('On Test Set:', 93.555555555555557)

In [8]:
data_train.target_names


Out[8]:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

In [ ]:


In [9]:
import matplotlib.pyplot as plt
plt.figure(figsize=(16,4))
for i,t in enumerate(data_train.target_names):
    plt.subplot(2,10,i+1)
    vector=random_vector(data_train,t)
    image.vector_to_image(vector,(8,8))
    plt.axis('off')
    
    plt.subplot(2,10,i+11)
    image.vector_to_image(C.weights[i,:],(8,8))
    plt.axis('off')


Do Backprop


In [10]:
C=BackProp(hidden_layer_sizes = [12])

In [11]:
timeit(reset=True)
C.fit(data_train.vectors,data_train.targets)
print(("Training time: ",timeit()))


Time Reset
('Training time: ', '0.26855993270874023 seconds ')

In [12]:
print(("On Training Set:",C.percent_correct(data_train.vectors,data_train.targets)))
print(("On Test Set:",C.percent_correct(data_test.vectors,data_test.targets)))


('On Training Set:', 100.0)
('On Test Set:', 95.333333333333343)

In [13]:
len(C.layers_coef_)


Out[13]:
2

In [14]:
C.layers_coef_[0].shape,C.layers_coef_[1].shape


Out[14]:
((64, 12), (12, 10))

In [ ]: